<?php

namespace App\Http\Controllers\index;

use App\Http\Controllers\Controller;
use Auth;
use DB;
use Hashids;
use Illuminate\Http\Request;
use Carbon\Carbon;

class IndexController extends Controller
{
    /**
     * [index description]
     * @Author   weiyp
     * @DateTime 2017-07-12T18:03:25+0800
     * @return   [type]                   [description]
     */
    public function index()
    {
        $params['keyword'] = isset($request->keyword) ? $request->keyword : "";
        $params['area'] = "";
        $params['area_code'] = isset($request->area) ? $request->area : "";
        if (isset($request->area) && !empty($request->area)) {
            $city_code = explode('/', $request->area)[1];
            $area = DB::table('area')->select('code', 'name')->where('code', $city_code)->first();
            $params['area'] = !empty($area) ? $area->name : "";
        }
        return view('index.index', ['params' => $params]);
    }
     public function buy()
    {
        return view('index.buy');
    }
    public function sell()
    {
        return view('index.sell');
    }
    /** 采购详情搜索方法 post
     * @return mixed
     */
    public function json(Request $request)
    {
        $type=$request->type;
        $query = DB::table('information');
        $query->join('companies', 'information.user_id', '=', 'companies.user_id');
        $query->join('area', 'information.city', '=', 'area.code');
        $query->where('information.type', '=', $type);
        $query->where('information.status', '=', 1);
        if (isset($request->keyword) && !empty($request->keyword)) {
            $query->where('information.variety', 'like', '%' . $request->keyword . '%');
        }
        if (isset($request->area) && !empty($request->area)) {
            $area = explode('/', $request->area);

            if (isset($area[1]) && !empty($area[1])) {
                $query->where('information.city', '=', $area[1]);
            }
        }
        $data = $query->select('information.*', 'companies.name', 'companies.valid', 'companies.id as cid', 'area.name as city_name')->orderBy('updated_at', 'desc')->paginate(5);

        foreach ($data as $val) {
            $val->code_id = Hashids::encode($val->id);
            if ($val->expiration >= time()) {
                $val->expiration = floor(($val->expiration - strtotime($val->updated_at)) / 86400);
            } else {
                $val->expiration = 0;
            }
            $val->favorite=null;
            if (Auth::check()) {
                $val->favorite = \App\Models\Favorite::where(['user_id' => Auth::user()->id, 'information_id' => $val->id])->first();
            }
            unset($val->id);
            Carbon::setLocale('zh');
            $dt = Carbon::createFromTimestamp(strtotime($val->updated_at));
            $val->days = $dt->diffForHumans();
            $val->logo = DB::table('attachments')->where(['own' => 1, 'own_id' => $val->cid, 'type' => 'logo'])->first();
            $val->company_id = Hashids::encode($val->cid);
            unset($val->cid);
        }
        return response()->json($data);
    }
    public function echart()
    {
        dd('echart');
    }
}
